package com.wang.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wang.mall.bean.SysMenu;
import com.wang.mall.bean.SysRole;
import com.wang.mall.bean.SysRoleMenu;
import com.wang.mall.mapper.SysMenuMapper;
import com.wang.mall.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Autowired
    SysRoleService sysRoleService;

    @Autowired
    SysUserService sysUserService;

    @Autowired
    SysUserRoleService sysUserRoleService;

    @Override
    public List<SysMenu> getMenusByUserId(Long userId) {
        // 查询所有的角色
        List<SysRole> sysRoleList = sysRoleService.findByUserId(userId);
        if (CollectionUtils.isEmpty(sysRoleList)) {
            // 没有角色, 就是没有权限, 返回空的权限集合
            return Collections.emptyList();
        }

        List<String> roleNames = sysRoleList.stream().map(SysRole::getRoleName).collect(Collectors.toList());
        if (roleNames.contains("admin")) { // 超级管理员查询所有的权限
            return getAllMenuList(null);
        }

        List<Long> roleIds = sysRoleList.stream().map(SysRole::getRoleId).collect(Collectors.toList());

        // 有权限, 根据用户ID查询菜单信息
        List<SysMenu> menuByRoleIds = baseMapper.findMenuByRoleIds(roleIds);
        List<Long> menuIds = menuByRoleIds.stream().map(SysMenu::getMenuId).collect(Collectors.toList());
        return getAllMenuList(menuIds);
    }

    /**
     * 获取所有菜单列表
     */
    private List<SysMenu> getAllMenuList(List<Long> menuIdList) {
        //查询根菜单列表
        List<SysMenu> menuList = queryListParentId(0L, menuIdList);
        //递归获取子菜单
        getMenuTreeList(menuList, menuIdList);

        return menuList;
    }

    @Override
    public List<SysMenu> getUserMenuList() {
        Long userId = Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        List<SysRole> sysRoleList = sysRoleService.findByUserId(userId);
        List<String> collect = sysRoleList.stream().map(SysRole::getRoleName).collect(Collectors.toList());

        // 没有角色就没有权限
        if (CollectionUtils.isEmpty(sysRoleList)) {
            return Collections.emptyList();
        }


        if (collect.contains("admin")) {
            // 超级管理员,返回所有的菜单
            return getAllMenuList(null);
        }


        // 根据角色ID查询菜单
        List<Long> roldIds = sysRoleList.stream().map(SysRole::getRoleId).collect(Collectors.toList());

        List<SysMenu> menuByRoleIds = baseMapper.findMenuByRoleIds(roldIds);
        List<Long> collect1 = menuByRoleIds.stream().map(SysMenu::getMenuId).collect(Collectors.toList());
        return getAllMenuList(collect1);

    }

    @Override
    public Set<String> getPermissions(Set<String> collect) {
        Long userId = Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        List<SysRole> sysRoleList = sysRoleService.findByUserId(userId);
        List<String> roleName = sysRoleList.stream().map(SysRole::getRoleName).collect(Collectors.toList());

        // 没有角色就没有权限
        if (CollectionUtils.isEmpty(sysRoleList)) {
            return Collections.emptySet();
        }


        if (roleName.contains("admin")) {
            List<SysMenu> sysMenuList = list();
            // 超级管理员,返回所有的菜单
            return sysMenuList.stream().map(SysMenu::getPerms).collect(Collectors.toSet());
        }
        List<Long> roleIds = sysRoleList.stream().map(SysRole::getRoleId).collect(Collectors.toList());

        // 普通用户 跟据角色查询
        List<SysMenu> menuByRoleIds = baseMapper.findMenuByRoleIds(roleIds);

        return menuByRoleIds.stream().map(SysMenu::getPerms).collect(Collectors.toSet());
    }


    /**
     * 获取不包含按钮的菜单列表
     */
    @Override
    public List<SysMenu> queryNotButtonList() {
        return baseMapper.queryNotButtonList();
    }


    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    @Override
    public List<SysMenu> listByCurrentUser() {
        Long userId = Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        if (userId == 1) {
            return list();
        }
        List<Long> roleIdList = sysUserRoleService.queryRoleIdList(userId);
        List<SysRoleMenu> sysRoleMenuList = sysRoleMenuService.list(new LambdaQueryWrapper<SysRoleMenu>().in(SysRoleMenu::getRoleId, roleIdList));
        List<Long> menuIds = sysRoleMenuList.stream().map(SysRoleMenu::getMenuId).collect(Collectors.toList());

        return (List<SysMenu>) sysMenuService.listByIds(menuIds);
    }


    /**
     * 递归
     */
    private List<SysMenu> getMenuTreeList(List<SysMenu> menuList, List<Long> menuIdList) {
        List<SysMenu> subMenuList = new ArrayList<SysMenu>();

        for (SysMenu entity : menuList) {
            //目录
            if (entity.getType() == 0) {
                entity.setList(getMenuTreeList(queryListParentId(entity.getMenuId(), menuIdList), menuIdList));
            }
            subMenuList.add(entity);
        }

        return subMenuList;
    }


    public List<SysMenu> queryListParentId(Long parentId, List<Long> menuIdList) {
        List<SysMenu> menuList = queryListParentId(parentId);
        if (menuIdList == null) {
            return menuList;
        }

        List<SysMenu> userMenuList = new ArrayList<>();
        for (SysMenu menu : menuList) {
            if (menuIdList.contains(menu.getMenuId())) {
                userMenuList.add(menu);
            }
        }
        return userMenuList;
    }


    public List<SysMenu> queryListParentId(Long parentId) {
        return baseMapper.queryListParentId(parentId);
    }

}
